<?php

namespace services\patient;

use Yii;
use common\components\Service;
use common\helpers\CommonHelper;
use common\models\member\Attr;
use common\models\patient\MedicalAuxiliary;
use api\modules\v1\forms\patient\MedicalForm;

/**
 * Class MedicalAuxiliaryService
 * @package services\patient
 */
class MedicalAuxiliaryService extends Service
{
    /**
     * 根据ID查找信息
     *
     * @param int $id
     * @return MedicalAuxiliary|null
     */
    public function findById(int $id = 0): ?MedicalAuxiliary
    {
        return MedicalAuxiliary::find()->where(['id' => $id])->one();
    }

    /**
     * 查找信息
     *
     * @param array $condition
     * @return MedicalAuxiliary|null
     */
    public function findInfo(array $condition = []): ?MedicalAuxiliary
    {
        $query = MedicalAuxiliary::find();

        isset($condition['patient_user_id']) && $query->andWhere(['patient_user_id' => $condition['patient_user_id']]);
        isset($condition['body_parts_id']) && $query->andWhere(['body_parts_id' => $condition['body_parts_id']]);

        return $query->one();
    }

    /**
     * 查找列表
     *
     * @param array $condition
     * @return array|\yii\db\ActiveRecord[]
     */
    public function findList(array $condition = [])
    {
        $query = MedicalAuxiliary::find();

        isset($condition['patient_user_id']) && $query->andWhere(['patient_user_id' => $condition['patient_user_id']]);
        isset($condition['body_parts_id']) && $query->andWhere(['body_parts_id' => $condition['body_parts_id']]);
        isset($condition['status']) && $query->andWhere(['status' => $condition['status']]);

        return $query->asArray()->all();
    }

    /**
     * 患者病历辅助列表
     *
     * @param array $params
     * @param int $page
     * @param int $pageSize
     * @return array
     */
    public function getListPage(array $params = [], int $page = 0, int $pageSize = 0)
    {
        $query = MedicalAuxiliary::find()->select(['a.*', 'b.profession_name'])->alias('a')
            ->leftJoin(Attr::tableName().' AS b', 'b.member_id = a.operate_id AND b.project_id = a.project_id')
            ->where([
                'a.project_id' => $params['project_id'],
                'a.patient_user_id' => $params['patient_user_id'],
            ])
            ->orderBy(['a.created_at' => SORT_DESC]);

        // 总数
        $total = $query->count();

        // 分页
        if ($page && $pageSize) {
            $query->limit($pageSize)->offset(($page - 1) * $pageSize);
        }

        $list = $query->asArray()->all();

        return [
            'page' => (int)$page,
            'pageSize' => (int)$pageSize,
            'total' => (int)$total,
            'list' => $list,
        ];
    }

    /**
     * 格式化患者回访列表数据
     *
     * @param array $data
     * @param int $type
     * @return array
     */
    public function getListPageFormat(array $data)
    {
        $list = [];
        foreach ($data as $row) {
            $item = [
                'id' => $row['id'],
                'project_id' => $row['project_id'],
                'patient_user_id' => $row['patient_user_id'],
                'desc' => $row['desc'],
                'operate_id' => $row['operate_id'],
                'operate_name' => $row['operate_name'],
                'profession_name' => $row['profession_name'] ?: '',
                'created_at_str' => $row['created_at'] ? date('Y-m-d H:i', $row['created_at']) : '',// 本次回访时间
            ];

            $list[] = $item;
        }

        return $list;
    }

    /**
     * 新增病历辅助内容
     *
     * @param MedicalForm $model
     * @return array|mixed
     */
    public function create(MedicalForm $model)
    {
        try {
            // 1.新增病历辅助内容
            $medicalAuxiliary = new MedicalAuxiliary();
            $medicalAuxiliary->project_id = $model->project_id;
            $medicalAuxiliary->patient_user_id = $model->patient_user_id;
            $medicalAuxiliary->desc = $model->desc;
            $medicalAuxiliary->operate_id = Yii::$app->user->identity->member_id;
            $medicalAuxiliary->operate_name = Yii::$app->user->identity->member->realname;
            $medicalAuxiliary->save();

            return ['id' => $medicalAuxiliary->id];
        } catch (\Exception $e) {
            return CommonHelper::api(422, $e->getMessage());
        }
    }
}
